package edu.gym4all.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;

import com.example.gym4all.R;

import edu.gym4all.service.ExerciseEnum;
import edu.gym4all.service.domain.Hypertrophy;
import edu.gym4all.service.domain.Training;
import edu.gym4all.service.organizer.ExerciseOrganizerImpl;
import edu.gym4all.service.organizer.TrainingOrganizerImpl;

public class HyperExercisesActivity extends Activity {

	private ExerciseOrganizerImpl exerciseOrganizer = new ExerciseOrganizerImpl();

	private TrainingOrganizerImpl trainingOrganizer = new TrainingOrganizerImpl();

	private Map<String, ExerciseEnum[]> map = new HashMap<String, ExerciseEnum[]>();

	private List<Hypertrophy> exerciseList = new ArrayList<Hypertrophy>();

	private ArrayAdapter<Hypertrophy> adapterExerciseList;

	private Spinner spinner1;

	private Training training;

	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		this.setContentView(R.layout.hyper_exercises);

		this.exerciseOrganizer.populateMapExercises(this.map);

		this.spinner1 = (Spinner) this.findViewById(R.id.contextChooser1);

		ExerciseEnum[] exercises = this.map
				.get(MuscleTypeActivity.exerciseType);

		this.training = this.getIntent().getParcelableExtra("training");

		ArrayAdapter<ExerciseEnum> adapterExerciseDrop = new ArrayAdapter<ExerciseEnum>(
				this, android.R.layout.simple_spinner_dropdown_item, exercises);
		this.spinner1.setAdapter(adapterExerciseDrop);

		List<Hypertrophy> hyperListExisted = this.exerciseOrganizer
				.loadHyperExercises(this.training, this);

		// List<ItemListHyperExercise> itens = new
		// ArrayList<ItemListHyperExercise>();
		// for (HypertrophyExercise hyper : hyperListExisted) {
		// itens.add(new ItemListHyperExercise(hyper.getExercise(), hyper
		// .getSeries(), hyper.getReps(), new RadioButton(this)));
		// }
		//
		// AdapterListHyperExercises adapterlistHyperExercise = new
		// AdapterListHyperExercises(
		// this, itens);

		// ListView list = (ListView) this
		// .findViewById(R.id.listaExistedExercises);
		// list.setAdapter(adapterlistHyperExercise);

		for (Hypertrophy exercise : hyperListExisted) {
			this.exerciseList.add(exercise);
		}
		this.adapterExerciseList = new ArrayAdapter<Hypertrophy>(this,
				android.R.layout.simple_list_item_1, this.exerciseList);
		ListView listView = (ListView) this.findViewById(R.id.exerciseList);

		// Ver Esses dois sets, arrumar para ficar apenas um.
		// listView.setAdapter(adapterlistHyperExercise);
		listView.setAdapter(this.adapterExerciseList);

		Button buttonSave = (Button) this.findViewById(R.id.save);
		buttonSave.setOnClickListener(new OnClickButtonSave());
	}

	private class OnClickButtonSave implements OnClickListener {

		@Override
		public void onClick(View v) {
			EditText seriesEdit = (EditText) HyperExercisesActivity.this
					.findViewById(R.id.series);
			int series = Integer.valueOf(seriesEdit.getText().toString());

			EditText repsEdit = (EditText) HyperExercisesActivity.this
					.findViewById(R.id.reps);
			int reps = Integer.valueOf(repsEdit.getText().toString());

			ExerciseEnum exercise = (ExerciseEnum) HyperExercisesActivity.this.spinner1
					.getSelectedItem();

			// HyperExercisesActivity.this.exerciseOrganizer.createExercise(
			// exercise, series, reps,
			// HyperExercisesActivity.this.training);
			//
			// HyperExercisesActivity.this.trainingOrganizer
			// .saveTraining(HyperExercisesActivity.this.training);

			Hypertrophy hyperExercise = HyperExercisesActivity.this.exerciseOrganizer
					.createExercise(exercise, series, reps,
							HyperExercisesActivity.this.training);

			HyperExercisesActivity.this.exerciseList.add(hyperExercise);
			HyperExercisesActivity.this.adapterExerciseList
					.notifyDataSetChanged();
		}
	}
}